<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>商城商品购买数量增减的完美js效果</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<!-- html代码begin -->
<div class="bookNum">
    <a id="sub" href="javascript:void(0);">-</a>
    <input type="text" value="1" id="bookNum">
    <a id="add" href="javascript:void(0);">+</a>
    <a href="javascript:void(0);" id="addCart">加入借阅台</a>
    <div class="clear"></div>
    <input type="text" id="test"/>
</div>
<!-- html代码end -->
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script language="javascript">
    $(function(){
        $("#bookNum").keypress(function(b) {
            var keyCode = b.keyCode ? b.keyCode : b.charCode;
            if (keyCode != 0 && (keyCode < 48 || keyCode > 57) && keyCode != 8 && keyCode != 37 && keyCode != 39) {
                return false;
            } else {
                return true;
            }
        }).keyup(function(e) {
            var keyCode = e.keyCode ? e.keyCode : e.charCode;
            console.log(keyCode);
            if (keyCode != 8) {
                var numVal = parseInt($("#bookNum").val()) || 0;
                numVal = numVal < 1 ? 1 : numVal;
                $("#bookNum").val(numVal);
            }
        }).blur(function() {
            var numVal = parseInt($("#bookNum").val()) || 0;
            numVal = numVal < 1 ? 1 : numVal;
            $("#bookNum").val(numVal);
        });

        //增加
        $("#add").click(function() {
            var num = parseInt($("#bookNum").val()) || 0;
            $("#bookNum").val(num + 1);
        });

        //减去
        $("#sub").click(function() {
            var num = parseInt($("#bookNum").val()) || 0;
            num = num - 1;
            num = num < 1 ? 1 : num;
            $("#bookNum").val(num);
        });
    });
</script>
</body>
</html>